package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.guli.common.util.HttpClientUtils;
import com.atguigu.srb.base.exception.SrbException;
import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.base.util.SrbAssert;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.UserInfoService;

import com.atguigu.srb.core.service.WxLoginService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpRequest;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Controller
@RequestMapping("/api/wx")
public class ApiWxLoginController {

    @Resource
    UserInfoService userInfoService;

    @Resource
    WxLoginService wxLoginService;

    /*
        # 微信开放平台 appid
        appId: wxed9954c01bb89b47
        # 微信开放平台 appsecret
        appSecret: a7482517235173ddb4083788de60b90e
        # 微信开放平台 重定向url(localhost:8160 必须和wx平台配置的授权回调域一致)
        redirectUri: http://localhost:8160/api/ucenter/wx/callback
     */

    @GetMapping("/login")
    public String login(HttpSession session){

        return wxLoginService.login(session);

    }

    //此方法回调 wx用户已确认授权，他希望得到的相应结果是登录成功的页面
    //code=041GVtml2lypUe4nGqnl2SWPg54GVtmu&state=3d6be0a4035d839573b04816624a415e
    @GetMapping("/callback")
    public String callback(String code,//用户授权后的临时票据
                           String state,
                           HttpSession session, HttpServletRequest request) throws IOException, ParseException {//防止csrf跨站点攻击的唯一字符串
        return wxLoginService.callback(code,state,session,request);
    }
}
